<?php
namespace app\auth\controller;

use think\Controller;
use think\Db;

class Reg extends Controller
{
    public function handle()
    {
        // $data = input('post.');

        $name = array_filter(explode(' ', trim(input('name'))));
        // halt($name);
        try {
            Db::name('users')->insert([
                'first_name' => $name[0],
                'last_name' => $name[1] ?? '',
                'email' => input('email'),
                'phone' => input('phone'),
                'message' => input('message'),
                'contact_preference' => input('contact_by'),
                'created_date' => date("Y-m-d"),
                'created_by' => input('name')
            ]);
            $result = $this->welcomeEmail(input('email'), $name);
            echo $result;
            // echo 'ok';
        } catch (\Throwable $th) {
            echo $th->getMessage();exit;
        }
    }

    protected function welcomeEmail($toemail, $name)
    {
        $fName = $name[0];
        $lastN = isset($name[1]) ? strtoupper($name[1][0]) : '';
        $subject='Start a free trial';
        $fromName = config('email.from_username');
        $content= <<<heredoc
        <p>Hello $fName $lastN,</p>
        <p>Thanks for sign up our service. We will contact you within 1- 2 days and set up your cloud office.</p>
        <br/>
        Sincerely,
        <br/>
        $fromName
        <br/>
        COS service team
heredoc;
       return send_mail($toemail,join(' ', $name),$subject,$content);
    }

}
